package com.aps.algorithm.heuristic;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

@Component
public class StrategyContext implements InitializingBean {

    private final Map<String, GeneticAlgorithmStrategy> strategyMap = new HashMap<>();

    @Autowired
    private Set<GeneticAlgorithmStrategy> strategySet; // 会自动收集所有@Bean注册的实现类

    @Override
    public void afterPropertiesSet() {
        for (GeneticAlgorithmStrategy strategy : strategySet) {
            strategyMap.put(strategy.getStrategyType(), strategy);
        }
    }

    public GeneticAlgorithmStrategy getStrategy(String strategyType) {
        return strategyMap.getOrDefault(strategyType, strategyMap.get("default"));
    }
}
